home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / txf / src / txform.c < prev    next >
C/C++ Source or Header  |  1994-09-17  |  5KB  |  195 lines

  1. /*====================================================================
  2.  *
  3.  * TXF textfile format module
  4.  *
  5.  *====================================================================
  6.  *                                   copyright(C) 1992-1994 T.Nakatani
  7.  *====================================================================
  8.  */
  9. #include "txf.h"
  10.  
  11. #define writeCRLF(PTR,BUF,APTR,ABUF)  *(PTR++) = CR, *(PTR++) = LF, *PTR = NUL, _dos_write(outputh, BUF, strlen(BUF), &dmy), PTR = BUF, APTR = ABUF
  12. extern char far *buf_base;
  13. extern char far *bufrp;
  14. extern long int bufremain, bufyet, bufldval, bufoffset, buffsize;
  15.  
  16. void txform(void)
  17. {
  18.     int ktype, retflg = 0;
  19.     unsigned long int ix, iy, ox, oy;        /* 入桁,入行,出桁,出行 */
  20.     unsigned int tmp, dmy;                /* 読み込み量 */
  21.     char *wbuf, *watr;                /* Write buffer */
  22.     char far *rptr;                    /* Read pointer */
  23.     char *wptr, *watp;                /* Write pointer */
  24.     char *ptmp, *ptmp2, *ptmpa;
  25.  
  26.     if (viewmode > 1) {
  27.         fprintf(stderr, "TXF main module textfile formatter.\n");
  28.     }
  29. #ifdef DEBUG
  30.     if (viewmode > 5) {
  31.         fprintf(stderr, "ぶら下げ文字=%s(%Np)\n", kl,kl);
  32.         fprintf(stderr, "送り出し文字=%s(%Np)\n", kf,kf);
  33.         fprintf(stderr, "引用通知文字=%s(%Np)\n", kq,kq);
  34.     }
  35. #endif
  36.     rptr = buffileopen(1);
  37.     rptr[rbufsize-1] = CR;
  38.     rptr[rbufsize] = LF;
  39.     rptr[rbufsize+1] = NUL;
  40.     wbuf = malloc(max(84, maxc));
  41.     watr = malloc(max(84, maxc));
  42.     if ((wbuf == NULL) || (watr == NULL)) {
  43.         logicerr("out of memory. (txform)");
  44.     }
  45.  
  46.  
  47.     wptr = wbuf;
  48.     watp = watr;
  49.     rptr = readnext(1);
  50.     ix = iy = ox = oy = 1L;
  51.  
  52.     while (remainbyte()) {
  53. /*
  54.         printf("offset=%d, remain=%ld, ix=%ld, iy=%ld, ox=%ld, oy=%ld, yet=%ld, *rptr(+1)=%X%X(%c%c)\n", bufoffset,bufremain,ix,iy,ox,oy,bufyet, *rptr, *(rptr+1), *rptr, *(rptr+1));
  55. */
  56.  
  57.         if (ix == 1L) {
  58.             quote = chktouch(rptr, iy);
  59.         }
  60.         if (quote == 1) {
  61.             if (ox != 1) {
  62.                 writeCRLF(wptr, wbuf, watp, watr);
  63.                 ox = 1;
  64.                 oy++;
  65.             }
  66.             if ((bufoffset + (tmp = linelen(rptr))) <= rbufsize) {
  67.                 _dos_write(outputh, rptr, tmp+1, &dmy);
  68.                 rptr = readnext(tmp);
  69.                 if (remainbyte() > 0) {
  70.                     _dos_write(outputh, rptr+1, 1, &dmy);
  71.                     ix = 1L;
  72.                     iy++;
  73.                     ox = 1L;
  74.                     oy++;
  75.                     rptr = readnext(2);
  76.                 }
  77.             }
  78.             else {
  79.                 _dos_write(outputh, rptr, rbufsize-1 - bufoffset, &dmy);
  80.                 rptr = readnext(rbufsize-1 - bufoffset);
  81.                 while (*(rptr = readnext(1)) != LF) {
  82.                     _dos_write(outputh, rptr, 1, &dmy);
  83.                     if (!remainbyte()) break;
  84.                 }
  85.                 ix = 1L;
  86.                 iy++;
  87.                 ox = 1L;
  88.                 oy++;
  89.             }
  90.             quote = 0;
  91.         }
  92.         else {
  93.             if ((ox == 1) && (*rptr != CR) && (left > 0)) { /* -kl 処理 */
  94.                 for (; ox <= left; ox++) {
  95.                     *(wptr++) = ' ';
  96.                     *(watp++) = CT_ANK;
  97.                 }
  98.             }
  99.             ktype = CT_ANK;
  100.             while (((ox <= maxc) && (*rptr != CR)) || (ktype == CT_KJ1)) {
  101.                 *watp = ktype = chkctype(*rptr, ktype);
  102.                 *wptr = *rptr;
  103.                 wptr++;
  104.                 watp++;
  105.                 ix++;
  106.                 ox++;
  107.                 if (*rptr == TAB) {
  108.                     ox = ((int)((ox - 2) / tabsize) + 1) * tabsize + 1;
  109.                 }
  110.                 if (!remainbyte()) {
  111.                     break;
  112.                 }
  113.                 else {
  114.                     rptr = readnext(1);
  115.                 }
  116.  
  117.                 if ((setline > 0) && (ox == right) 
  118.                         && ((ox % 2) == 0) && (ktype == CT_KJ2)) {
  119.                     for (ptmp = wptr-1, ptmpa = watp-1;
  120.                             ptmp >= wbuf; ptmp--, ptmpa--) {
  121.                         if (*ptmpa == CT_ANK) {
  122.                             break;
  123.                         }
  124.                     }
  125.                     if ((*ptmpa == CT_ANK) && (ox > right-2)) {
  126.                         for (ptmp2 = wptr-1; ptmp2 > ptmp; ptmp2--) {
  127.                             *(ptmp2+1) = *(ptmp2);
  128.                         }
  129.                         *(ptmp2+1) = ' ';
  130.                         wptr++;
  131.                         ix += 1L;
  132.                         ox += 1L;
  133.  
  134.                         for (ptmp2 = watp-1; ptmp2 > ptmpa; ptmp2--) {
  135.                             *(ptmp2+1) = *(ptmp2);
  136.                         }
  137.                         *(ptmp2+1) = CT_ANK;
  138.                         watp++;
  139. /*---*
  140.                         if (rptr < rbuf) {
  141.                             printf("\nError:Parameter Dump:offset=%d, remain=%ld, ix=%ld, iy=%ld, ox=%ld, oy=%ld, yet=%ld", offset,remain,ix,iy,ox,oy,yet);
  142.                             logicerr(NULL);
  143.                         }
  144. *---*/
  145.                     }
  146.                 }
  147.                 if (ox > (right - 3) && (ktype != CT_KJ1)) {
  148.                     if (*rptr == CR) {
  149.                         break;
  150.                     }
  151.                     if (putret(ox, rptr)) {
  152.                         writeCRLF(wptr, wbuf, watp, watr);
  153.                         ox = 1;
  154.                         oy++;
  155.                         retflg = 1;
  156.  
  157.                         break; /* いらないはず */
  158.  
  159.                     }
  160.                 }
  161.             }
  162.  
  163.             if (*rptr == CR) {
  164.                 if (((ix == 1L) || findret(ox, iy, rptr) || (remainbyte() < 3L)) && !retflg) {
  165.                 
  166.                     writeCRLF(wptr, wbuf, watp, watr);
  167.                     ox = 1;
  168.                     oy++;
  169.                 }
  170.                 ix = 1L;
  171.                 iy++;
  172.                 rptr = readnext(2);
  173.             }
  174. /*
  175.             if (ox > right-3) {
  176.                 if (putret(ox, rptr)) {
  177.                     writeCRLF(wptr, wbuf, watp, watr);
  178.                     ox = 1;
  179.                     oy++;
  180.                 }
  181.             }
  182. */
  183.             retflg = 0;
  184.         }
  185.     }
  186.     if (ox != 1) {
  187.         *wptr = NUL;
  188.         _dos_write(outputh, wbuf, strlen(wbuf), &dmy);
  189.     }
  190.  
  191.     free(wbuf);
  192.     free(watr);
  193.     bufclose();
  194. }
  195.